﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace CodepageMaintain
{
	partial class AutoCastCollection<T>
	{
		partial class SafeAutoCastCollection : AutoCastCollection<T>,
			IEnumerable<T>
		{
			#region Конструктор

			public SafeAutoCastCollection(object underlayingCollection)
				: base(underlayingCollection)
			{
			}

			#endregion

			#region IEnumerator<T> Members

			IEnumerator<T> IEnumerable<T>.GetEnumerator()
			{
				if (EnumerableTyped)
				{
					(_asEnumerableTyped).GetEnumerator();
				}

				return new SafeAutoCastEnumerator(((IEnumerable)this).GetEnumerator());
			}

			#endregion

			#region Перегруженные методы

			public override T this[int index]
			{
				get
				{
					if (this.List)
					{
						var obj = ((IList)this)[index];
						if (obj is T)
						{
							return (T)obj;
						}

						return default(T);
					}

					return base[index];
				}
				set { base[index] = value; }
			}

			public override AutoCastCollection<T> SafeCast
			{
				get
				{
					return this;
				}
			}

			#endregion
		}
	}
}
